package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.SignInfo;
import com.baomidou.ant.entity.User;
import com.baomidou.ant.service.ISignInfoService;
import com.baomidou.ant.service.IUserService;
import com.baomidou.ant.utils.compareDate;
import com.baomidou.ant.vo.Request.RequestSignInVo;
import com.baomidou.ant.vo.Response.ResponseSignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/sign-info")
public class SignInfoController {
    @Autowired
    private ISignInfoService signInfoService;
    @Autowired
    private IUserService userService;

    //签到界面打开接口
    @GetMapping("/signInUI/{userId}")
    public Result signInUI(@PathVariable Integer userId) {
        //找到用户的签到纪录
        QueryWrapper<SignInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        SignInfo signInfo=signInfoService.getOne(queryWrapper);
        //添加返回类
        ResponseSignInfoVo responseSignInfoVo=new ResponseSignInfoVo();
        //如果用户没有签到过，为他生成一条专属的签到纪录
        if(ObjectUtils.isEmpty(signInfo)){
            SignInfo info=new SignInfo();
            info.setUserId(userId);
            info.setSignTimes(0);
            signInfoService.save(info);
            return Result.getSuccess().setData(responseSignInfoVo);
        }
        //判断上次签到时间
        if(signInfo.getSignIn()==null){
            responseSignInfoVo.setSignIn(null);
        }else
            responseSignInfoVo.setSignIn(signInfo.getSignIn());
        responseSignInfoVo.setSignTimes(signInfo.getSignTimes());
        //获取当前时间
        LocalDateTime date=LocalDateTime.now();
        //返回结果
        if(compareDate.dateToYMD(date,signInfo.getSignIn())){//今天签到过
          return Result.getFailure().setData(responseSignInfoVo);
        }else
            return Result.getSuccess().setData(responseSignInfoVo);
    }



    //签到接口
    @PostMapping("/signIn")
    public Result signIn(@RequestBody RequestSignInVo requestSignInVo){
        if(signInfoService.sign(requestSignInVo)){
            //成功签到之后，给予积分奖励
            QueryWrapper<SignInfo> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_id",requestSignInVo.getUserId());
            SignInfo signInfo=signInfoService.getOne(queryWrapper);
            int p;//纪录奖励倍数
            if(signInfo.getSignTimes()<=7){
                p=signInfo.getSignTimes();
            }else
                p=signInfo.getSignTimes()%7;
            if(p==0){
                p=7;
            }
            User user=userService.getById(requestSignInVo.getUserId());
            user.setPoints(user.getPoints()+(p*10));
            userService.saveOrUpdate(user);
            return Result.getSuccess().setMsg("签到成功,获得积分奖励!");
        }else
            return Result.getFailure().setMsg("签到失败,今天已经签到了!");
    }

}
